package com.yl.chainStore.control.sick

import com.yl.chainStore.common.Constants
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.sick.SickMedicalHistory
import com.yl.chainStore.entity.user.CompanyUserMedicalHistoryModel
import com.yl.chainStore.entity.user.CompanyUserMedicalHistoryModelType
import com.yl.chainStore.pojo.vo.sick.SickMedicalHistorySelectVo
import com.yl.chainStore.services.*
import com.yl.chainStore.services.sick.ICompanyUserMedicalHistoryModelTypeService
import com.yl.chainStore.services.sick.ISickMedicalHistoryService
import com.yl.chainStore.util.medicalHistoryTypeToTree
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

/**
 * 客户病历界面
 */
@RestController
@RequestMapping("sick/history")
open class CustomerMedicalHistoryController(
	private val iCompanyUserMedicalHistoryModelTypeService: ICompanyUserMedicalHistoryModelTypeService,
	private val iCustomerInfoService: ICustomerInfoService,
	private val iSickMedicalHistoryService: ISickMedicalHistoryService,
	private val iCompanyDictionaryService:ICompanyDictionaryService,
	private val iWebCompanyDeptService: IWebCompanyDeptService
):BaseCtrl() {


	/**
	 * 获取公司的病历模板  类型（树形）-模板
	 */
	@GetMapping("getCompanyTypeModel")
	@Transactional
	open fun getCompanyTypeModel():ResponseBody{
		val medicalHistoryTypeModelList = iWebCompanyDeptService.getMedicalHistoryTypeModelList()
		return returnSuccess(medicalHistoryTypeToTree(medicalHistoryTypeModelList,0L))
	}

	/**
	 * 获取公司的病历模板类型 树形
	 */
	@GetMapping("getCompanyTypeTree")
	@Transactional
	open fun getCompanyTypeTree():ResponseBody{
		val medicalHistoryTypeModelList = iWebCompanyDeptService.getMedicalHistoryTypeList()
		return returnSuccess(medicalHistoryTypeToTree(medicalHistoryTypeModelList,0L))
	}

	/**
	 * 添加模板类型
	 */
	@PostMapping("addModelType")
	@Transactional(rollbackFor = [Exception::class])
	open fun addModelType(@RequestBody modelType: CompanyUserMedicalHistoryModelType):ResponseBody{
		val addMedicalHistoryModelType = iWebCompanyDeptService.addMedicalHistoryModelType(modelType)
		if(addMedicalHistoryModelType.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return addMedicalHistoryModelType
	}

	/**
	 * 添加模板
	 */
	@PostMapping("addModel")
	@Transactional(rollbackFor = [Exception::class])
	open fun addModel(@RequestBody model: CompanyUserMedicalHistoryModel):ResponseBody{
		val addMedicalHistoryModel = iCompanyUserMedicalHistoryModelTypeService.addModel(model)
		if(addMedicalHistoryModel.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return addMedicalHistoryModel
	}



	/**
	 * 修改模板类型
	 */
	@PostMapping("updateModelType")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateModelType(@RequestBody modelType: CompanyUserMedicalHistoryModelType):ResponseBody{
		val updateMedicalHistoryModelType = iCompanyUserMedicalHistoryModelTypeService.updateModelType(modelType)
		if(updateMedicalHistoryModelType.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return updateMedicalHistoryModelType
	}

	/**
	 * 修改模板
	 */
	@PostMapping("updateModel")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateModel(@RequestBody model: CompanyUserMedicalHistoryModel):ResponseBody{
		val updateModel = iCompanyUserMedicalHistoryModelTypeService.updateModel(model)
		if(updateModel.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return updateModel
	}


	/**
	 * 删除模板类型
	 */
	@PostMapping("delModelType/{id}")
	@Transactional(rollbackFor = [Exception::class])
	open fun delModelType(@PathVariable id:Long):ResponseBody{
		val delType = iCompanyUserMedicalHistoryModelTypeService.delType(id)
		if(delType.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return delType
	}

	/**
	 * 删除模板
	 */
	@PostMapping("delModel/{id}")
	@Transactional(rollbackFor = [Exception::class])
	open fun delModel(@PathVariable id:Long):ResponseBody{
		val delModel = iCompanyUserMedicalHistoryModelTypeService.delModel(id)
		if(delModel.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return delModel
	}

	/**
	 * 获取病历列表
	 */
	@PostMapping("getMedicalHistoryList")
	@PermsCode("customer:sick:history:show")
	@Transactional
	open fun getMedicalHistoryList(@RequestBody vo : SickMedicalHistorySelectVo):ResponseBody{
		return returnSuccess(iCustomerInfoService.getMedicalHistoryList(vo))
	}

	/**
	 * 添加病历 (全部患者处)
	 */
	@PostMapping("addMedicalHistory")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("customer:sick:history:add")
	open fun addMedicalHistory(@RequestBody medicalHistory: SickMedicalHistory):ResponseBody{
		val addMedicalHistory = iCustomerInfoService.addMedicalHistory(medicalHistory)
		if(addMedicalHistory.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return addMedicalHistory
	}

	/**
	 * 修改病历(全部患者处)
	 */
	@PostMapping("updateMedicalHistory")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("customer:sick:history:update")
	open fun updateMedicalHistory(@RequestBody medicalHistory: SickMedicalHistory):ResponseBody{
		val updateMedicalHistory = iSickMedicalHistoryService.updateMedicalHistory(medicalHistory)
		if(updateMedicalHistory.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return updateMedicalHistory
	}

	/**
	 * 删除病历(全部患者处)
	 */
	@PostMapping("delMedicalHistory/{id}")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("customer:sick:history:del")
	open fun delMedicalHistory(@PathVariable id: Long):ResponseBody{
		val delMedicalHistory = iSickMedicalHistoryService.delMedicalHistory(id)
		if(delMedicalHistory.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return delMedicalHistory
	}

	/**
	 * 获取病历标签
	 */
	@GetMapping("getMedicalHistoryLabel")
	@Transactional
	open fun getMedicalHistoryLabel(): ResponseBody {
		return returnSuccess(
			iCompanyDictionaryService.getCompanyDetailListToMapIdName(
				Constants.DistId.SICK_MEDICAL_HISTORY_LABEL,
				getContext().companyId,
				getContext().subCompanyId
			)
		)
	}




	/**
	 * 获取病历列表
	 */
	@PostMapping("getHistorySelect")
	@Transactional
	open fun getHistorySelect(@RequestBody vo : SickMedicalHistorySelectVo):ResponseBody{
		val list = mutableListOf<Map<String, Any>>()

		for (sickMedicalHistory in iCustomerInfoService.getMedicalHistoryList(vo)) {
			val map = mutableMapOf<String,Any>()
			map["id"]=sickMedicalHistory.id
			map["name"]=sickMedicalHistory.actionChief?:""
			list.add(map)
		}
		return returnSuccess(list)
	}

	/**
	 * 获取修改记录
	 */
	@GetMapping("getLogListById/{id}")
	@Transactional
	open fun getLogListById(@PathVariable id:Long):ResponseBody{
		return returnSuccess(iSickMedicalHistoryService.getHistoryOperationLog(id))
	}




}
